package com.sunrise.gateway.commons;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class Result {
    private String code;
    private String message;
    private Object data;
    private ResultContext context = new ResultContext();

    public Result() {
    }
    public Result(String code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public String toJsonString() {
        if (data instanceof String) {
            StringBuilder sb = new StringBuilder("{");

            sb.append("\"code\":");
            if (null == code) {
                sb.append("null");
            } else {
                sb.append('"').append(code).append('"');
            }

            sb.append(",\"message\":");
            if (null == message) {
                sb.append("null");
            } else {
                sb.append('"').append(message).append('"');
            }

            sb.append(",\"data\":").append(data);    // json数据, 做特殊处理

            sb.append(",\"context\":").append(context.toJsonString());

            sb.append('}');

            return sb.toString();
        } else {
            return JSON.toJSONString(this, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
        }
    }
    @Override
    public String toString() {
        return toJsonString();
    }


    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
    public ResultContext getContext() {
        return context;
    }
    public void setContext(ResultContext context) {
        this.context = context;
    }
}
